package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.MessageDao;
import com.fuli.app.dto.MessageDTO;
import com.fuli.app.dto.query.MessageQueryDTO;
import com.fuli.app.entity.Message;
import com.fuli.app.service.MessageService;
import com.fuli.app.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageDao messageDao;

    @Override
    public ActionResult<String> add(MessageDTO dto) {
        String id = dto.getId();
        Message message = messageDao.get(id);
        if (message != null)
            return new ActionResult<>(-1, "添加失败，id已存在");
        //一个订单号最多报名三次
        int ordercount = messageDao.getCountByOrder(dto.getOrderId());
        if (ordercount >= 3)
            return new ActionResult<>(-1, "添加失败，同一订单已经报名三次");
        //同一个微信号不能重复提交
      List  <Message> message1 = messageDao.getByOrder(dto.getOrderId());
      for(int i=0;i<message1.size();i++) {
          if (message1 != null) {
              if (message1.get(i).getOpenId().equals(dto.getOpenId()))
                  return new ActionResult<>(-1, "该微信号已经提交报名过");
          }
      }
        Message po = EntityUtils.copyObjectWithInit(dto, Message.class);
        po.setDealed(false);
        po.setId(id);
        po.setDealTime(null);
        messageDao.insert(po);
        return new ActionResult<>(po.getId());
    }

    @Override
    public ActionResult<String> delete(String id) {
        Message message = messageDao.get(id);
        if (message == null)
            return new ActionResult<>(id);
        messageDao.delete(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> batchDelete(List<String> ids) {
        messageDao.batchDelete(ids, SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<MessageDTO> get(String id) {
        Message message = messageDao.get(id);
        MessageDTO dto = EntityUtils.copyObject(message, MessageDTO.class);
        return new ActionResult<>(dto);
    }

    @Override
    public ActionResult<Pagination<MessageDTO>> listByQueryDTO(MessageQueryDTO queryDTO) {
        Pagination pagination = new Pagination(queryDTO.getPageNo(), queryDTO.getPageSize());
        int count = messageDao.getCountByQuery(queryDTO);
        pagination.setCount(count);
        if (count > 0) {
            List<Message> messages = messageDao.listByQueryDTO(queryDTO);
            List<MessageDTO> dtos = EntityUtils.copyList(messages, MessageDTO.class);
            pagination.setRows(dtos);
            return new ActionResult<>(pagination);
        }
        return new ActionResult<>(pagination);
    }

    @Override
    public ActionResult<String> deal(String id) {
        messageDao.deal(id,SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> batchDeal(List<String> ids) {
        messageDao.batchDeal(ids, SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<Pagination<MessageDTO>> listAllByQueryDTO(MessageQueryDTO queryDTO) {
        Pagination pagination = new Pagination(queryDTO.getPageNo(), queryDTO.getPageSize());
        int count = messageDao.getCountAllByQuery(queryDTO);
        pagination.setCount(count);
        if (count > 0) {
            List<Message> messages = messageDao.listAllByQueryDTO(queryDTO);
            List<MessageDTO> dtos = EntityUtils.copyList(messages, MessageDTO.class);
            pagination.setRows(dtos);
            return new ActionResult<>(pagination);
        }
        return new ActionResult<>(pagination);
    }
}
